package top.yuki.data;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import top.yuki.LevelWorld;


import java.io.File;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class TempDataManage {
    public HashMap<String, HashMap>  tempDataMap = new HashMap();
    private  ConfigurationSection config;
    private LevelWorld lw;
    private Date refreshTime;
    public static TempDataManage init(LevelWorld lw) {

        return new TempDataManage(lw);
    }          		//简单生成对象的方法

    public TempDataManage(LevelWorld lw) {
        refreshTime = new Date();
        this.lw = lw;
        config = lw.getConfig();
    }
// 提醒true 不提醒false
    public void initPlayerData(String id){
        if (!tempDataMap.containsKey(id)) {
            HashMap map = new HashMap();
            map.put("ExpLimitRemind",true);
            map.put("dayExpLimitRemind",true);
            tempDataMap.put(id,map);
        }

    }

    public void updateExpLimitRemind(String id, boolean ExpLimitRemind){
        tempDataMap.get(id).put("ExpLimitRemind",ExpLimitRemind);
    }
    public void updateDayExpLimitRemind(String id, boolean ExpLimitRemind){
        tempDataMap.get(id).put("dayExpLimitRemind",ExpLimitRemind);

    }

    public boolean getExpLimitRemind(String id){

            boolean expLimitRemind = (boolean) tempDataMap.get(id).get("ExpLimitRemind");
            return expLimitRemind;

    }
    public boolean getDayExpLimitRemind(String id){

            boolean expLimitRemind = (boolean) tempDataMap.get(id).get("dayExpLimitRemind");
            return expLimitRemind;

    }

    public void cleanData(){
        tempDataMap = new HashMap();
    }

    public void tryRefreashData(){
        int refreashDay = (int) config.get("refreashDay");
        if (differentDays(new Date()) >= refreashDay) {
            cleanData();
        }
    }
    /**
     * date2比date1多的天数
     * @param date1
     * @return
     */
    public int differentDays(Date date1)
    {

        Date date2 = refreshTime;

        if(date1.after(date2)){
            Date date3=date1;
            date1=date2;
            date2=date3;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //不同年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //同年
        {

            return day2-day1;
        }
    }

}
